package com.fsx.serverimmediate.comm;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * 放映厅
 */
@Component
@ServerEndpoint("/audiovisual/{filmName}")
public class AvSocket {
    private static final Map<String,CopyOnWriteArraySet<Session>> SESSION_POOL=new HashMap<>();
    private static final Map<String,Double> PACE_POOL=new HashMap<>();
    @OnOpen
    public void onOpen(Session session, @PathParam("filmName") String filmName){
        if(SESSION_POOL.containsKey(filmName)){
            SESSION_POOL.get(filmName).add(session);
            System.out.println("【WebSocket消息】有新的连接，总数为：" + SESSION_POOL.get(filmName).size());
        }else {
            CopyOnWriteArraySet<Session> set=new CopyOnWriteArraySet<>();
            set.add(session);
            SESSION_POOL.put(filmName,set);
            PACE_POOL.put(filmName,0.0);
            System.out.println("【WebSocket消息】有新的连接，总数为：" + SESSION_POOL.get(filmName).size());
        }
    }
    @OnClose
    public void onClose(Session session,@PathParam("filmName") String filmName){
        SESSION_POOL.get(filmName).remove(session);
        if(SESSION_POOL.get(filmName).size()==0){
            SESSION_POOL.remove(filmName);
            PACE_POOL.remove(filmName);
        }
    }

    @OnMessage
    public void onMessage(String message,@PathParam("filmName") String filmName,Session session){
        System.out.println("【WebSocket消息】收到客户端消息："+ message);
        String[] array = StringUtils.delimitedListToStringArray(message,"#");
            switch (array[0]){
                case "paceAsk":
                    sendAllMessage("paceAsk",filmName);
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    break;
                case "paceCall":
                    System.out.println("paceDower#"+PACE_POOL.get(filmName));
                    session.getAsyncRemote().sendText("paceDower#"+PACE_POOL.get(filmName));
                    break;
                case "paceSync":
                    double pace=PACE_POOL.get(filmName);
                    if(pace<Double.valueOf(array[1])){
                        PACE_POOL.put(filmName,Double.valueOf(array[1]));
                    }
                    break;
                case "interact":
                    sendAllMessage("interact#"+array[1],filmName);
                    break;
            }
    }
    public void sendAllMessage(String message,String filmName){
        CopyOnWriteArraySet<Session> sessions=SESSION_POOL.get(filmName);
        for (Session session : sessions) {
            if(session.isOpen()){
                session.getAsyncRemote().sendText(message);
            }
        }
    }
}
